<?php

namespace Rexzy\Tool;

class Page {

    /**
     * 分页功能
     * 
     * @param int $total 总共要显示的结果量
     * @param int $rang 显示页面按钮的数量
     * @param int $page 现在显示的是第几页
     * @param int $length 每页显示的结果量
     * @param string $condition 筛选条件 
     */
    public function paging($total, $rang = 7, $page, $length, $url, $condition = array()) {
        $total_page = ceil($total / $length);
        $half_rang = ( $rang - 1 ) / 2;
        if ( count( $condition ) != 0 ) {
            $pass_condition = "&" .  http_build_query($condition);
        } else {
            $pass_condition = "";
        }

        if ($page == 1) {
            echo "<li class='disabled'><a href='#' aria-label='Previous'><span aria-hidden='true'>&lt;&lt;</span></a></li>";
            echo "<li class='disabled'><a href='#' aria-label='Previous'><span aria-hidden='true'>&lt;</span></a></li>";
        } else {
            echo "<li><a href='" . $url . "?page=1" . $pass_condition . "'><b>&lt;&lt;</b></a></li>";
            echo "<li><a href='" . $url . "?page=" . ( $page - 1 ) . $pass_condition . "'><b>&lt;</b></a></li>";
        }

        if ($total_page <= $rang) {
            for ($i = 1; $i <= $total_page; $i++) {
                if ($i == $page) {
                    echo "<li class='active'><a>" . $i . "</a></li>";
                } else {
                    echo "<li><a href='" . $url . "?page=" . $i . $pass_condition . "'>" . $i . "</a></li>";
                }
            }
        } else {
            if ( ( $page - $half_rang ) <= 1) {
                for ($i = 1; $i <= $rang; $i++) {
                    if ($i == $page) {
                        echo "<li class='active'><a>" . $i . "</a></li>";
                    } else {
                        echo "<li><a href='" . $url . "?page=" . $i . $pass_condition . "'>" . $i . "</a></li>";
                    }
                }
            } else if ( ( $page + $half_rang ) >= $total_page) {
                for ( $i = ( ($total_page - $rang) + 1 ); $i <= $total_page; $i++) {
                    if ($i == $page) {
                        echo "<li class='active'><a>" . $i . "</a></li>";
                    } else {
                        echo "<li><a href='" . $url . "?page=" . $i . $pass_condition . "'>" . $i . "</a></li>";
                    }
                }
            } else {
                for ($i = ( $page - $half_rang); $i <= ( $page + $half_rang ); $i++) {
                    if ($i == $page) {
                        echo "<li class='active'><a>" . $i . "</a></li>";
                    } else {
                        echo "<li><a href='" . $url . "?page=" . $i . $pass_condition . "'>" . $i . "</a></li>";
                    }
                }
            }
        }

        if ($page == $total_page) {
            echo "<li class='disabled'><a href='#' aria-label='Previous'><span aria-hidden='true'>&gt;</span></a></li>";
            echo "<li class='disabled'><a href='#' aria-label='Previous'><span aria-hidden='true'>&gt;&gt;</span></a></li>";
        } else {
            echo "<li><a href='" . $url . "?page=" . ( $page + 1 ) . $pass_condition . "'><b>&gt;</b></a></li>";
            echo "<li><a href='" . $url . "?page=" . $total_page . $pass_condition . "'><b>&gt;&gt;</b></a></li>";
        }
    }

}
